package com.fengmi.pay.config;

import com.github.wxpay.sdk.WXPayRequest;
import com.github.wxpay.sdk.WXPayUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class Demo {

    //测试统一下单接口
    public static void main(String[] args) throws Exception{

        WXConfig wxConfig = new WXConfig();

        //1：指定请求参数
        Map requestParamMap = new HashMap();
        requestParamMap.put("appid",wxConfig.getAppID());
        requestParamMap.put("mch_id",wxConfig.getMchID());
        requestParamMap.put("nonce_str", WXPayUtil.generateNonceStr());
        requestParamMap.put("body","下单");
        requestParamMap.put("out_trade_no","11111011");
        requestParamMap.put("total_fee",100+"");
        requestParamMap.put("spbill_create_ip","127.0.0.1");
        requestParamMap.put("notify_url","http://www.baidu.com");
        requestParamMap.put("trade_type","NATIVE");

        //2：max转xml格式
        String requestParamXML = WXPayUtil.generateSignedXml(requestParamMap, wxConfig.getKey());


        //3:发送请求
        WXPayRequest request = new WXPayRequest(wxConfig);
        String responseXML = request.requestWithoutCert("/pay/unifiedorder", UUID.randomUUID().toString(), requestParamXML, false);



        //4:responseXML转成map
        Map<String, String> responseMap = WXPayUtil.xmlToMap(responseXML);




        //5:获取响应结果
        System.out.println(responseXML);
        System.out.println( responseMap.get("code_url"));


    }

    //测试查询订单状态接口
//    public static void main(String[] args) throws Exception {
//
//
//        //1:构建配置对象
//        WXConfig config = new WXConfig();
//        //2:封装请求参数
//        Map params = new HashMap();
//        params.put("appid",config.getAppID());
//        params.put("mch_id",config.getMchID());
//        params.put("out_trade_no","111110000");
//        params.put("nonce_str",WXPayUtil.generateNonceStr());
//        //3:map转xml
//        String requestXML = WXPayUtil.generateSignedXml(params, config.getKey());
//        //4:发送请求WXPayRequest request = new WXPayRequest(config);
//        WXPayRequest request = new WXPayRequest(config);
//        String resXml = request.requestWithoutCert("/pay/orderquery", UUID.randomUUID().toString(),
//                requestXML, false);
//
//        System.out.println(resXml);
//
//        //5:响应结果转map集合
//        Map<String, String> resMap = WXPayUtil.xmlToMap(resXml);
//        //6:获取支付状态
//        System.out.println(resMap.get("trade_state"));
//
//
//
//    }

//测试关闭订单接口
//    public static void main(String[] args) throws  Exception {
//        //1:构建配置对象
//        WXConfig config = new WXConfig();
////2:封装请求参数
//        Map params = new HashMap();
//        params.put("appid",config.getAppID());
//        params.put("mch_id",config.getMchID());
//        params.put("out_trade_no", "111110000");
//        params.put("nonce_str",WXPayUtil.generateNonceStr());
////3:map转xml
//        String reqeustXML = WXPayUtil.generateSignedXml(params, config.getKey());
////4:发送请求WXPayRequest request = new WXPayRequest(config);
//        WXPayRequest request = new WXPayRequest(config);
//        String resXML = request.requestWithoutCert("/pay/closeorder", UUID.randomUUID().toString(),
//                reqeustXML, false);
////5:响应结果转map集合
//        Map<String, String> resMap = WXPayUtil.xmlToMap(resXML);
////6:获取关闭结果
//        System.out.println(resMap.get("result_code"));
//        System.out.println(resMap.get("result_msg"));
//
//    }



}
